package com.tianzeculture.stagebao.activity.business;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tianzeculture.stagebao.R;
import com.tianzeculture.stagebao.application.StageBaoApplication;
import com.tianzeculture.stagebao.base.BaseActivity;
import com.tianzeculture.stagebao.bean.Notice;
import com.tianzeculture.stagebao.bean.NoticeList;
import com.tianzeculture.stagebao.bean.ResultBean;
import com.tianzeculture.stagebao.bean.WorkTag;
import com.tianzeculture.stagebao.net.BusinessRestUsage;
import com.tianzeculture.stagebao.net.TagRestUsage;
import com.tianzeculture.stagebao.network.Request;
import com.tianzeculture.stagebao.network.enity.MSG;
import com.tianzeculture.stagebao.network.util.ToastUtil;
import com.tianzeculture.stagebao.util.StatusBarUtil;
import com.tianzeculture.stagebao.util.Util;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/7/5 0005.
 * 发布通告2
 */

public class PublishNoticeTwoActivity extends BaseActivity {
    private int HOT_TAG = 101;
    private int SAVE_ANNOUNCEMENT = 102;

    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.edtContent)
    EditText edtContent;

    @BindView(R.id.layoutOneself)
    LinearLayout layoutOneself;
    @BindView(R.id.tvStyleTag)
    TextView tvStyleTag;
    @BindView(R.id.layoutStyleTag)
    LinearLayout layoutStyleTag;
    @BindView(R.id.btnPublish)
    Button btnPublish;
    @BindView(R.id.boxTravel)
    CheckBox boxTravel;
    @BindView(R.id.boxClothing)
    CheckBox boxClothing;

    @Request
    BusinessRestUsage businessRestUsage = null;

    Notice notice = null;
    List<String> checkTags = null;

    boolean recruit;
    NoticeList noticeList;

    @Override
    public void initUI() {
        super.initUI();
        StatusBarUtil.setWindowStatusBarColor(this, R.color.colorGray_F8);
        StageBaoApplication.addActivity(this);

        tvTitle.setText(getString(R.string.publish_notice));

        boolean oneself = getIntent().getBooleanExtra("oneself", false);
        notice = (Notice) getIntent().getSerializableExtra("notice");
        recruit = getIntent().getBooleanExtra("isRecruit", false);
        noticeList = (NoticeList) getIntent().getSerializableExtra("noticelist");

        if (oneself) {
            layoutOneself.setVisibility(View.VISIBLE);
        } else {
            layoutOneself.setVisibility(View.GONE);
        }
        if (recruit && noticeList != null) {
            edtContent.setText(noticeList.getContent());
            if (noticeList.getIscostume().equals("1"))
                boxClothing.setChecked(true);
            if (noticeList.getIstravel().equals("1"))
                boxTravel.setChecked(true);
        }
    }


    @OnClick({R.id.tvStyleTag, R.id.btnPublish})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tvStyleTag:
                Intent intent = new Intent(PublishNoticeTwoActivity.this, WorkTagActivity.class);
                intent.putExtra("checkTag", (Serializable) checkTags);
                startActivityForResult(intent, Util.WORK_TAG);
                break;
            case R.id.btnPublish:

                if ("".equals(edtContent.getText().toString().trim())) {
                    ToastUtil.showShort(this, "请填写工作内容!");
                } else {
                    if (recruit) {
                        noticeList.setContent(edtContent.getText().toString().trim());
                        if (boxTravel.isChecked())
                            noticeList.setIstravel("1");
                        else
                            noticeList.setIstravel("0");
                        if (boxClothing.isChecked())
                            noticeList.setIscostume("1");
                        else
                            noticeList.setIscostume("0");
                        if (checkTags == null)
                            checkTags = new ArrayList<>();
                        if (checkTags.size() > 0) {
                            StringBuffer buffer = new StringBuffer();
                            for (int i = 0; i < checkTags.size(); i++) {
                                buffer.append(checkTags.get(i) + ",");
                            }
                            String s = buffer.toString().substring(0, buffer.length() - 1);
                            noticeList.setTag(s);
                        }
                        businessRestUsage.updateAnnouncement(SAVE_ANNOUNCEMENT, noticeList);
                    } else {
                        notice.setJobContent(edtContent.getText().toString().trim());
                        if (boxTravel.isChecked())
                            notice.setIstravel("1");
                        else
                            notice.setIstravel("0");
                        if (boxClothing.isChecked())
                            notice.setIscostume("1");
                        else
                            notice.setIscostume("0");
                        if (checkTags == null)
                            checkTags = new ArrayList<>();
                        if (checkTags.size() > 0) {
                            StringBuffer buffer = new StringBuffer();
                            for (int i = 0; i < checkTags.size(); i++) {
                                buffer.append(checkTags.get(i) + ",");
                            }
                            String s = buffer.toString().substring(0, buffer.length() - 1);
                            notice.setWorkNote(s);
                        }
                        businessRestUsage.saveAnnouncement(SAVE_ANNOUNCEMENT, notice);
                    }
                }
                break;
        }
    }

    @Override
    public void refreshUI(int taskId, MSG msg) {
        super.refreshUI(taskId, msg);
        if (taskId == SAVE_ANNOUNCEMENT) {
            ToastUtil.showShort(this,msg.getMessage());
            if (msg.getData()) {
                if (recruit){
                    startActivity(new Intent(this,BusinessHomepagerActivity.class));
                }
                StageBaoApplication.finishAllActivity();

            }
        }
    }

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_publish_notice_two;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Util.HOT_TAG) {
            Bundle bundle = data.getExtras();
            checkTags = bundle.getStringArrayList("checkTag");
            updateUI(layoutStyleTag, checkTags);
        }
    }

    private void updateUI(LinearLayout layout, List<String> tags) {
        layout.removeAllViews();
        for (int i = 0; i < tags.size(); i++) {
            View view = LayoutInflater.from(this).inflate(R.layout.item_work_tag, null);
            CheckBox boxWorkTag = view.findViewById(R.id.boxWorkTag);
            boxWorkTag.setChecked(true);
            boxWorkTag.setClickable(false);
            boxWorkTag.setText(tags.get(i));
            view.setPadding(0, 0, 25, 0);
            layout.addView(view);
        }
    }
}
